package org.hnxxxy.ebms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.hnxxxy.ebms.common.Result;
import org.hnxxxy.ebms.mapper.PermissionMapper;
import org.hnxxxy.ebms.mapper.RolePermissionMapper;
import org.hnxxxy.ebms.pojo.Role;
import org.hnxxxy.ebms.pojo.RolePermission;
import org.hnxxxy.ebms.service.RolePermissionService;
import org.hnxxxy.ebms.vo.StuRolePermissionVo;
import org.hnxxxy.ebms.vo.TeaRolePermissionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WANGWEI
 * @since 2021-06-15
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public Result<String> del(Integer roleId, Integer permissionId) {
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId).eq("permission_id",permissionId);
        if (this.remove(queryWrapper)) {
            return Result.success(200,"删除成功");
        }
        return Result.fail("删除失败");
    }

    @Override
    public Result<IPage<StuRolePermissionVo>> getStuPermission(int page) {
        // 构建分页
        IPage<StuRolePermissionVo> iPage = new Page<>(page,10);
        IPage<StuRolePermissionVo> stuRolePermissionVoIPage = rolePermissionMapper.selectRolePermissionPage(iPage);
        return Result.success(stuRolePermissionVoIPage);
    }

    @Override
    public Result<IPage<TeaRolePermissionVo>> getTeaPermission(int page) {
        IPage<TeaRolePermissionVo> iPage = new Page<>(page,10);
        IPage<TeaRolePermissionVo> rolePermissionVoIPage = rolePermissionMapper.selectTeaRolePermissionPage(iPage);
        return Result.success(rolePermissionVoIPage);
    }

    @Override
    public Result<String> editRecPer(StuRolePermissionVo stuRolePermissionVo) {
        RolePermission rolePermission = new RolePermission();
        rolePermission.setId(stuRolePermissionVo.getId());
        rolePermission.setRoleId(stuRolePermissionVo.getRoleId());
        rolePermission.setPermissionId(stuRolePermissionVo.getPermissionId());
        if (this.updateById(rolePermission)) {
            return Result.success(200,"修改成功");
        }
        return Result.fail("修改失败");
    }

    @Override
    public Result<String> editTeaPer(TeaRolePermissionVo teaRolePermissionVo) {
        RolePermission rolePermission = new RolePermission();
        rolePermission.setId(teaRolePermissionVo.getId());
        rolePermission.setRoleId(teaRolePermissionVo.getRoleId());
        rolePermission.setPermissionId(teaRolePermissionVo.getPermissionId());
        if (this.updateById(rolePermission)) {
            return Result.success(200,"修改成功");
        }
        return Result.fail("修改失败");
    }

    @Override
    public Result<IPage<StuRolePermissionVo>> getStuPerName(String name, int page) {
        IPage<StuRolePermissionVo> iPage = new Page<>(page,10);
        IPage<StuRolePermissionVo> stuRolePermissionVoIPage = rolePermissionMapper.getStuPerName(iPage,name);
        return Result.success(stuRolePermissionVoIPage);
    }

    @Override
    public Result<IPage<TeaRolePermissionVo>> getTeaPerName(String name, int page) {
        IPage<TeaRolePermissionVo> iPage = new Page<>(page,10);
        IPage<TeaRolePermissionVo> teaRolePermissionVoIPage = rolePermissionMapper.getTeaPerName(iPage,name);
        return Result.success(teaRolePermissionVoIPage);
    }
}
